console.log(THREE.REVISION)
$(function() {
	draw();

	if (navigator.userAgent.match(/mobile/i)) {
		$("#gyro").show().find("i").click(() => {
			$(this).toggleClass("icon-tuoluoyi");
			$(this).toggleClass("icon-t3jinyong");
			controlsState = !controlsState;

			initControl();

		})
	} else {
		$("#gyro").hide();
	}


})
var renderer, camera, scene;
var controls;
//切换控制器
var controlsState = true;

/***************************************/
const radius = 50 // 球体半径
var VrRender; //创建一个双屏渲染器，当处于双屏状态，则使用双屏渲染器进行渲染
var isVR = false
var sound; //重点展位音频
var clock = new THREE.Clock();
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var OrbitAutoRotate = false;

var poiObjects = [];

/***************************************/




function initRender() {
	renderer = new THREE.WebGLRenderer({
		antialias: true
	});
	renderer.setSize(window.innerWidth, window.innerHeight);
	//告诉渲染器需要阴影效果
	renderer.shadowMap.enabled = true;
	renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
	document.getElementById("container").appendChild(renderer.domElement);
}

function initCamera() {
	camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
	camera.position.set(0, 0, 0.1);
	camera.lookAt(new THREE.Vector3(0, 0, 0));


}

function initScene() {
	scene = new THREE.Scene();
	scene.background = new THREE.Color(0xa0a0a0);
}


function initRoom() {
	initModels(0)
}



function initControl() {
	if (controlsState) {
		if (controls) {
			controls.dispose()
		}
		controls = new THREE.OrbitControls(camera, renderer.domElement);
        //是否可以缩放
		controls.enableZoom = true;

	} else if (controlsState === false) {

		if (controls) {
			controls.dispose()
		}
		controls = new THREE.DeviceOrientationControls(camera);
		controls.connect();
	}

}

function render() {

	controls.update();

	renderer.render(scene, camera);
}

function onWindowResize() {

	camera.aspect = window.innerWidth / window.innerHeight;
	camera.updateProjectionMatrix();
	renderer.setSize(window.innerWidth, window.innerHeight);

}

function animate() {
	//更新控制器
	render();


	requestAnimationFrame(animate);
}

function onMouseClick(event) {

	mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
	mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

	//将平面坐标系转为世界坐标系
	raycaster.setFromCamera(mouse, camera);

	//得到点击的几何体
	var raycasters = raycaster.intersectObjects(poiObjects, false);
	if (raycasters.length > 0) {

		Toast(raycasters[0].object.name, 2000);

		if (raycasters[0].object.vid) {
			clearnSprite()
			initModels(raycasters[0].object.vid)

			$('.imgNavUl li').removeClass("addLiStyle");
			$(".imgNavUl li").eq(raycasters[0].object.vid).addClass("addLiStyle addLiVisited");
			$('.imgNavUl li p span').removeClass("scroll-text");
			$(".imgNavUl li").eq(raycasters[0].object.vid).find("span").addClass("scroll-text");
		} else {
			console.log(raycasters[0].object);
			let sel = raycasters[0].object;
			$(".mianSceneContentWrap").show();
			$('.mianSceneHead h2').text(sel.textContent.title);
			$('.mianSceneContent p').text(sel.textContent.content);
			if (sel.textContent.audioPath) {
				addAudio(sel.textContent.audioPath)

			}

		}

	}

}

function onDocumentTouchStart(a) {
	// a.preventDefault();
	passive: false;
	if (a.touches.length == 1) {

		mouse.x = (a.touches[0].pageX / window.innerWidth) * 2 - 1;
		mouse.y = -(a.touches[0].pageY / window.innerHeight) * 2 + 1;

		//将平面坐标系转为世界坐标系
		raycaster.setFromCamera(mouse, camera);

		//得到点击的几何体
		var raycasters = raycaster.intersectObjects(poiObjects, false);
		if (raycasters.length > 0) {

			Toast(raycasters[0].object.name, 2000);

			if (raycasters[0].object.vid) {
				clearnSprite()
				initModels(raycasters[0].object.vid)

				$('.imgNavUl li').removeClass("addLiStyle");
				$(".imgNavUl li").eq(raycasters[0].object.vid).addClass("addLiStyle addLiVisited");
				$('.imgNavUl li p span').removeClass("scroll-text");
				$(".imgNavUl li").eq(raycasters[0].object.vid).find("span").addClass("scroll-text");
			} else {
				console.log(raycasters[0].object);
				let sel = raycasters[0].object;
				$(".mianSceneContentWrap").show();
				$('.mianSceneHead h2').text(sel.textContent.title);
				$('.mianSceneContent p').text(sel.textContent.content);
				if (sel.textContent.audioPath) {
					addAudio(sel.textContent.audioPath)

				}

			}

		}

	}
}

function draw() {
	initRender();
	initScene();
	initCamera();
	initRoom()
	initControl();


	animate();
	window.onresize = onWindowResize;
	document.getElementById("container").addEventListener("click", onMouseClick, false);
	document.getElementById("container").addEventListener("touchstart", onDocumentTouchStart, false);

}
